package hust.music.action;

import hust.music.action.base.ActionBase;
import hust.music.entity.User;
import hust.music.service.UserModService;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * MusicUploadAction
 * 
 * @author Txazo
 * @since 1.0
 */
@Controller
@Scope("session")
public class UpdateImageAction extends ActionBase {

	private static final long serialVersionUID = 1L;

	/** 文件 */
	private File upload;
	/** 文件名称 */
	private String uploadFileName;
	/** 文件类型 */
	private String uploadContentType;
	/** 文件存储路径 */
	private String savePath;
	/** 允许上传的文件类型 */
	private String allowTypes;
	/** 允许上传的文件大小 */
	private long maxSize;

	/** 提示消息 */
	private String tip;

	@Autowired
	private UserModService userModService;

	/**
	 * 上传图片
	 * 
	 * @return 逻辑视图
	 */
	@Override
	public String execute() throws Exception {
		System.out.println(this.getUploadContentType());
		System.out.println(this.getUploadFileName());
		if (this.getUpload().length() < this.getMaxSize()) {
			if (filterTypes(this.getAllowTypes().split(","))) {
				File file = new File(getSavePath());

				User user = (User) session.get("user");
				String path = user.getAvatar();
				System.out.println(path);
				if (!path.equals("./images/user/default.jpg")) {
					File oldFile = new File(file + "//" + user.getId() + ".gif");
					oldFile.delete();
					path = "./images/user/" + user.getId() + ".gif";
					System.out.println(path);
					user.setAvatar(path);
					userModService.update(user);
					session.put("user", user);
				}else{
					path = "./images/user/" + user.getId() + ".gif";
					System.out.println(path);
					user.setAvatar(path);
					userModService.update(user);
					session.put("user", user);
				}

				FileOutputStream fos = new FileOutputStream(file + "//"
						+ user.getId() + ".gif");
				FileInputStream fis = new FileInputStream(getUpload());
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
				fis.close();
				fos.close();

				tip = "恭喜你，上传成功";
			} else {
				tip = "上传文件类型错误";
			}
		} else {
			tip = "上传文件太大";
		}

		return SUCCESS;
	}

	public boolean filterTypes(String[] types) {
		String fileType = getUploadContentType();
		for (String type : types) {
			if (type.equals(fileType)) {
				return true;
			}
		}
		return false;
	}

	public String getSavePath() throws Exception {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public String getPath() throws Exception {
		return this.savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getAllowTypes() {
		return allowTypes;
	}

	public void setAllowTypes(String allowTypes) {
		this.allowTypes = allowTypes;
	}

	public long getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(long maxSize) {
		this.maxSize = maxSize;
	}

}